package ecom.stateless;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;

import ecom.api.*;

import ecom.entity.ConverterEntity;
import ecom.stateful.PanierStateful;

@Stateless(mappedName = "ecom.stateless.ConverterStateless@Remote", description = "ConverterStateless", name = "ConverterStateless")
public class ConverterStateless implements IConverterStateless {
	
	private Log log = LogFactory.getLog(PanierStateful.class);


	@PersistenceContext
    private EntityManager m_EntityManager = null; 
	
	private List<Double> valeursDevice = new ArrayList();
	 
	private List<String> nomsDevice;
	
	
	public void setValeursDevice(List<Double> valeursDevice) {
		this.valeursDevice = valeursDevice;
	}

	public List<Double> getValeursDevice()
	    {
			log.info("getValeursDevice()");
	    	List<ConverterEntity> listConverter = m_EntityManager.createQuery("select t from ConverterEntity t").getResultList();
	    	valeursDevice.clear();
	    	for(ConverterEntity converter : listConverter)
	    	{
	    		log.info("converter.getValue()"+converter.getValue());
	    		valeursDevice.add(converter.getValue());
	    	}
	    	return valeursDevice;	    	 
	    }
	
	public List<String> getNomsDevice()
    {
		log.info("getNomsDevice");
    	List<ConverterEntity> listConverter = m_EntityManager.createQuery("select t from ConverterEntity t").getResultList();
    	
    	List<String> result = new ArrayList<String> ();
    	for(ConverterEntity converter : listConverter)
    	{
    		result.add(converter.getNameDevice());
    	}
    	nomsDevice = result;
    	return result;	    	 
    }
	
	/**
	 * 
	 * @param nameDevice nom de la device 
	 * @param value la nouvelle valeur de la device
	 * 
	 */
	public void changeValue(String nameDevice, Double value){
		List<ConverterEntity> listConverter = m_EntityManager.createQuery("select t from ConverterEntity t").getResultList();
		long idDevice=-1;
		for(ConverterEntity converter : listConverter)
    	{
			if(converter.getNameDevice().equals(nameDevice))
    			idDevice = converter.getId();   		
    	}
		ConverterEntity modifDevice = new ConverterEntity();
		modifDevice.setId(idDevice);
		modifDevice.setNameDevice(nameDevice);
		modifDevice.setValue(value);
		m_EntityManager.merge(modifDevice);
		
	}

	
}
